package com.web.service.user;

import com.web.bean.user.User;
import com.web.dao.user.UserDaoImpl;
import com.web.utils.DBUtil;

import java.sql.Connection;
import java.util.List;


/**
 * @autor Chen
 * @create 2021--25-14:17
 * @desc
 */
public class UserServiceImpl implements UserService {


    /*
        selectUser
        addUser
        updateUser
        deleteUser
        selectUserByPhone
        addWriter
        updateWriter
        selectWriterByPhone
         */
    @Override
    public List<User> selectUsers() throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection = dbUtil.getConnection();
        UserDaoImpl userDao = new UserDaoImpl(connection);
        List<User> users = userDao.selectUsers();
        dbUtil.close();
        return users;
    }

    @Override
    public void addUser(User user) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection = dbUtil.getConnection();
        UserDaoImpl userDao = new UserDaoImpl(connection);
        userDao.addUser(user);
        dbUtil.close();
    }

    @Override
    public void updateUser(User user) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection = dbUtil.getConnection();
        UserDaoImpl userDao = new UserDaoImpl(connection);
        userDao.updateUser(user);
        dbUtil.close();
    }

    @Override
    public void deleteUser(String uid) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection = dbUtil.getConnection();
        UserDaoImpl userDao = new UserDaoImpl(connection);
        userDao.deleteUser(uid);
        dbUtil.close();
    }

    @Override
    public void selectUserByPhone(String phone) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection = dbUtil.getConnection();
        UserDaoImpl userDao = new UserDaoImpl(connection);
        userDao.selectUserByPhone(phone);
        dbUtil.close();

    }

    @Override
    public void addWriter(User user) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection = dbUtil.getConnection();
        UserDaoImpl userDao = new UserDaoImpl(connection);
        userDao.updateWriter(user);
        dbUtil.close();
    }

    @Override
    public void updateWriter(User user) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection = dbUtil.getConnection();
        UserDaoImpl userDao = new UserDaoImpl(connection);
        userDao.updateWriter(user);
        dbUtil.close();
    }

    @Override
    public void selectWriterByPhone(String phone) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection = dbUtil.getConnection();
        UserDaoImpl userDao = new UserDaoImpl(connection);
        userDao.selectWriterByPhone(phone);
        dbUtil.close();

    }

}
